package com.wolfsea.defineviewgrouppaper
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import androidx.viewbinding.ViewBinding
import com.wolfsea.defineviewgrouppaper.presenter.BasePresenter
import com.wolfsea.defineviewgrouppaper.view.IView

open abstract class MVPActivity<V : IView, P : BasePresenter<V>, VB : ViewBinding>(open val block: (LayoutInflater) -> VB) : AppCompatActivity(), IView {

    private lateinit var _binding: VB
    protected lateinit var presenter: P
    protected val mBinding get() = _binding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        _binding = block(layoutInflater)
        setContentView(_binding.root)
        init(savedInstanceState)
        presenter = getPresenter()
        presenter.attachView(this as V)
    }

    override fun onDestroy() {
        presenter.detachView()
        super.onDestroy()
    }

    protected abstract fun init(savedInstanceState: Bundle?)

    protected abstract fun getPresenter() : P

    protected fun setPresenter(presenter: P) {
        this.presenter = presenter
    }
}